<?php
require_once 'Xiachufang/logic/Recipe.php';
class IndexController extends BaseController  {
	/**
	 * 首页
	 */
	function indexAction() {
		require_once 'Xiachufang/logic/Tag.php';
		require_once 'Xiachufang/util/TimeUtil.php';
		// 获取大家关注的菜谱
		$recipeIdList = RecipeLogic::getHotRecipeIds(5);
		$topRecipe = false;
		if (count($recipeIdList) > 0) {
			$topRecipeId = array_shift($recipeIdList);
			
			$topRecipeEntity = new RecipeEntity();
			if ($topRecipeEntity->get($topRecipeId)) {
				$topRecipe = $topRecipeEntity->getDataArray(array("recipeId","name","description","costTime","tags","favNum","doneNum"));
				$topRecipe["tagList"] = TagLogic::getTagList($topRecipeEntity->tags);
			}
		}
		$recipeList = array();
		foreach($recipeIdList as $recipeId) {
			$recipe = new RecipeEntity();
			if (!$recipe->get($recipeId)) continue;
			$recipeList[] = $recipe->getDataArray(array("recipeId","name","description","favNum","doneNum"));
		}
		// 获取最新的菜谱
		$recipeIdList = RecipeLogic::getNewRecipeIds(4);
		$recipeList2 = array();
		foreach($recipeIdList as $recipeId) {
			$recipe = new RecipeEntity();
			if (!$recipe->get($recipeId)) continue;
			$recipeList2[] = $recipe->getDataArray(array("recipeId","name","favNum","doneNum", "description"));
		}
		
		// 获取最新的食物
		require 'Xiachufang/logic/Food.php';
		$foodIdList = FoodLogic::getLastFoodIds(4);
		$foodList = array();
		foreach($foodIdList as $foodId) {
			$food = new FoodEntity();
			if (!$food->get($foodId)) continue;
			$actorName = "";
			$actor = new ActorEntity();
			if ($actor->get($food->actorId)) $actorName = $actor->nickname;
			$data = $food->getDataArray(array("foodId", "name", "description","updateTime"));
			$data["actorName"] = $actorName;
			$data["createTime"] = TimeUtil::format($food->createTime);
			$foodList[] = $data;
		}
		
		// 获取用户最近的活动
		require_once 'Xiachufang/logic/Event.php';
		$eventList = EventLogic::getLastEvents(5);
		
		// 获取使用最多的标签
		$tagList = TagLogic::getHotTags();
		
		$this->smarty->assign(array(
			"pageTitle"=>"首页",
			"nickname"=>"王大嫂",			
		));
		
		$this->smarty->assign_by_ref("topRecipe", $topRecipe);
		$this->smarty->assign_by_ref("recipeList", $recipeList);
		$this->smarty->assign_by_ref("recipeList2", $recipeList2);
		$this->smarty->assign_by_ref("foodList", $foodList);
		$this->smarty->assign_by_ref("eventList",$eventList);
		$this->smarty->assign_by_ref("tagList",$tagList);
		$this->display();
	}
	
	/**
	 * 登录
	 */
	function loginAction() {
		$this->assign('pageTitle', '登录');
		$this->display();
	}
	
	/**
	 * 注销
	 *
	 */
	function logoutAction() {
		$rurl = $this->_getParam('rurl');
		if (empty($rurl)) $rurl = '/';
		ActorLogic::logout();
		$this->_redirect($rurl);
	}
	
	/**
	 * 注册
	 *
	 */
	function registerAction() {
		$this->assign('pageTitle', '注册');
		$this->display();
	}
	
	/**
	 * 验证图片
	 */
	function validcodeAction() {
		$validCodeKey = 'valid_code';
		session_start();
		//生成验证码图片
		header("Content-type: image/gif");
		srand((double)microtime()*1000000);//播下一个生成随机数字的种子，以方便下面随机数生成的使用
		//session_start();//将随机数存入session中
		$im = imagecreate(56,20) or die("Cant's initialize new GD image stream!");  //制定图片背景大小
		$red = ImageColorAllocate($im, 255,0,0); //设定三种颜色
		$white = ImageColorAllocate($im, 255,255,255);
		$gray = ImageColorAllocate($im, 200,200,200);
		$bule= ImageColorAllocate($im,12,41,250);
		//imagefill($im,0,0,$gray); //采用区域填充法，设定（0,0）
		imagefill($im,0,0,$white);//ed
		//生成数字和字母混合的验证码方法
		$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
		$list=explode(",",$ychar);
		for($i=0;$i<4;$i++){
		  $randnum=rand(0,35);
		  $authnum.=$list[$randnum]."";//ed 加入一个空格
		}
		//while(($authnum=rand()%100000)<10000); //生成随机的四位数
		//将四位整数验证码绘入图片
		$_SESSION[$validCodeKey]=$authnum;
		//int imagestring(resource image,int font,int x,int y,string s, int col)
		imagestring($im, 5, 10, 2, $authnum, $bule);
		//用col颜色将字符串s画到image所代表的图像的x，y座标处（图像的左上角为0,0）。
		//如果 font 是 1，2，3，4 或 5，则使用内置字体
		
		for($i=0;$i<400;$i++){ //加入干扰象素 {
		$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		// imagesetpixel($im, rand()%90 , rand()%30 , $randcolor);
		imagesetpixel($im, rand()%90 , rand()%30 , $gray);
		}
		ImagePNG($im);
		ImageDestroy($im);
	}
	
	/**
	 * 显示人的头像
	 *
	 */
	function faceAction() {
		$actorId = $_GET["a"];
		if (!is_numeric($actorId) || $actorId < 1) {
			notFound();			
		}
		echo <<<HTML
		<html>
		<head>
		<style type="text/css">
			body{margin:0;padding:0;}
		</style>
		</head>
		<body>
		<img src="/static/picture/people/{$actorId}"></img>
		<iframe scrolling="no"></iframe>
		</body>
		</html>
HTML;
	}
}
?>